export default {
	wxPay: function(url, data) {
		return new Promise((resolve, reject) => {
			let type = "";
			// #ifdef APP
			type = "app";
			// #endif

			// #ifdef H5
			type = "wap";
			if (uni.$jswx.isWechat()) {
				type = "mp";
			}
			// #endif

			// #ifdef MP
			type = "mini";
			// #endif

			data.type = type;
			uni.gPost(url, data).then((res) => {
				if (type == "wap" && res.h5_url) {
					window.location.href = res.h5_url;
					resolve()
				} else if (type == "mp") {
					uni.$jswx.wxpay(res, () => {
						uni.showToast({
							title: "支付成功"
						})
						resolve()
					}, (cancel) => {
						uni.showToast({
							title: "支付已取消",
							icon: "none"
						})
						reject()
					}, (fail) => {
						uni.showToast({
							title: "支付失败",
							icon: "none"
						})
						reject()
					});
				} else if (type == "mini") {
					uni.$jswx.wxpay(res, () => {
						uni.showToast({
							title: "支付成功"
						})
						resolve()
					}, (cancel) => {
						uni.showToast({
							title: "支付已取消",
							icon: "none"
						})
						reject()
					}, (fail) => {
						uni.showToast({
							title: "支付失败",
							icon: "none"
						})
						reject()
					});
				} else if (type == "app") {
					uni.requestPayment({
						provider: "wxpay",
						orderInfo: res,
						success: function(res) {
							uni.showToast({
								title: "支付成功"
							})
							resolve()
						},
						fail: function(err) {
							reject()
						}
					})
				} else {
					reject(res)
				}
			});

		})

	},
	balancePay: function(url, data) {
		data['type'] = "balance";
		return new Promise((resolve, reject) => {
			uni.gPost(url, data).then((res) => {
				resolve(res);
			}).catch((e) => {
				reject(e)
			});
		})

	},
	pay: function(url, data, pay_type) {
		if (pay_type == 1 || pay_type == 7) {
			return new Promise((resolve, reject) => {
				let type = "";
				// #ifdef APP
				type = "app";
				// #endif

				// #ifdef H5
				type = "wap";
				if (uni.$jswx.isWechat()) {
					type = "mp";
				}
				// #endif

				// #ifdef MP
				type = "mini";
				// #endif

				data.type = type;
				if (pay_type == 7) data.type1 = 'yeepay'
				uni.gPost(url, data).then((res) => {
					if (type == "wap" && res.h5_url) {
						window.location.href = res.h5_url;
						resolve()
					} else if (type == "mp") {
						uni.$jswx.wxpay(res, () => {
							uni.showToast({
								title: "支付成功"
							})
							resolve()
						}, (cancel) => {
							uni.showToast({
								title: "支付已取消",
								icon: "none"
							})
							reject()
						}, (fail) => {
							uni.showToast({
								title: "支付失败",
								icon: "none"
							})
							reject()
						});
					} else if (type == "mini") {
						uni.requestPayment({
							provider: "wxpay",
							timeStamp: res.timeStamp,
							nonceStr: res.nonceStr,
							package: res.package,
							signType: res.signType,
							paySign: res.paySign,
							success: function(res) {
								uni.showToast({
									title: "支付成功"
								})
								resolve()
							},
							fail: function(err) {
								if (err.errMsg == "requestPayment:fail cancel") {
									uni.showToast({
										title: "支付取消",
										icon: "error"
									})
								}
								reject()
							}
						})

					} else if (type == "app") {
						uni.requestPayment({
							provider: "wxpay",
							orderInfo: res,
							success: function(res) {
								uni.showToast({
									title: "支付成功"
								})
								resolve()
							},
							fail: function(err) {
								reject()
							}
						})
					} else {
						reject(res)
					}
				});
			})
		} else if (pay_type == 3) {
			data.type = "wjs_money";
			return new Promise((resolve, reject) => {
				uni.gPost(url, data).then((res) => {
					uni.showToast({
						title: "支付成功"
					})
					setTimeout(() => {
						resolve(res);
					}, 1500)

				}).catch((e) => {
					reject(e)
				});
			})
		} else if (pay_type == 7) { //第三方支付
			data.type = "dsfzf";
			return new Promise((resolve, reject) => {
				uni.gPost(url, data).then((res) => {
					setTimeout(() => {
						resolve(res);
					}, 50)

				}).catch((e) => {
					reject(e)
				});
			})
		} else if (pay_type == 5) { //第四方支付
			data.type = "di_si_fang";
			return new Promise((resolve, reject) => {
				uni.gPost(url, data).then((res) => {
					setTimeout(() => {
						resolve(res);
					}, 50)

				}).catch((e) => {
					reject(e)
				});
			})
		} else if (pay_type == 6) { //支付传参
			data.type = "xxzf";
			return new Promise((resolve, reject) => {
				uni.gPost(url, data).then((res) => {
					setTimeout(() => {
						resolve(res);
					}, 50)

				}).catch((e) => {
					reject(e)
				});
			})
		} else if (pay_type == 3) {
			data.type = "qyzf";
			return new Promise((resolve, reject) => {
				uni.gPost(url, data).then((res) => {
					setTimeout(() => {
						resolve(res);
					}, 50)

				}).catch((e) => {
					reject(e)
				});
			})
		} else if (pay_type == 4) {
			data.type = "zrdf";
			return new Promise((resolve, reject) => {
				uni.gPost(url, data).then((res) => {
					setTimeout(() => {
						resolve(res);
					}, 50)

				}).catch((e) => {
					reject(e)
				});
			})
		} else {
			data.type = "balance";
			return new Promise((resolve, reject) => {
				uni.gPost(url, data).then((res) => {
					uni.showToast({
						title: "支付成功"
					})
					setTimeout(() => {
						resolve(res);
					}, 1500)

				}).catch((e) => {
					reject(e)
				});
			})
		}
	}

}